package com.example.mywanandroid.ui.adapter;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class FmPagerAdapter extends FragmentPagerAdapter {

    private Fragment mCurrentFragment;
    private List<Fragment> fragmentList = new ArrayList<>();
    private List<String> sTitle = new ArrayList<>();

    public FmPagerAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentList) {
        super(fm);
        this.fragmentList = fragmentList;
    }

    public FmPagerAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentList, List<String> sTitle) {
        super(fm);
        this.fragmentList = fragmentList;
        this.sTitle = sTitle;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        if (sTitle != null && sTitle.size() != 0) {
            return sTitle.get(position);
        } else {
            return null;
        }
    }

    @Override
    public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        mCurrentFragment = (Fragment) object;
        super.setPrimaryItem(container,position,object);
    }

    public Fragment getCurrentFragment(){
        return mCurrentFragment;
    }
}
